package com.xhc.demo.repository;

import com.xhc.demo.entity.Address;
import com.xhc.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {
    List<Address> findByUser(User user);

    @Modifying
    @Query("UPDATE Address a SET a.isDefault = false WHERE a.user.userId = :userId AND a.addressid != :addressId")
    void updateOtherAddressesDefaultStatus(Long userId, Long addressId);

    Address findByAddressidAndUser(Long addressid, User user);
}
